home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13408 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Variant Records in C ... Is there a way ?
  5. Date: Sat, 06 Apr 96 18:24:41 GMT
  6. Organization: none
  7. Message-ID: <828815081snz@genesis.demon.co.uk>
  8. References: <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt> <315006F8.639@cmt.lpr.mail.carel.fi> <4iumf8$604@madeline.INS.CWRU.Edu> <danpop.827534657@rscernix> <4jcrbm$g5p@madeline.INS.CWRU.Edu> <828145096snz@genesis.demon.co.uk> <4jvdhj$mag@madeline.INS.CWRU.Edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jvdhj$mag@madeline.INS.CWRU.Edu>
  15.            mab22@po.CWRU.Edu "Michael A. Balfour" writes:
  16.  
  17. >In a previous article, fred@genesis.demon.co.uk (Lawrence Kirby) says:
  18. >
  19. >>In article <4jcrbm$g5p@madeline.INS.CWRU.Edu>
  20. >>           mab22@po.CWRU.Edu "Michael A. Balfour" writes:
  21. >>
  22. >>>
  23. >>>In a previous article, danpop@mail.cern.ch (Dan Pop) says:
  24. >>>
  25. >>>>In <4iumf8$604@madeline.INS.CWRU.Edu> mab22@po.CWRU.Edu (Michael A. Balfour)
  26. >>> writes:
  27. >>>>
  28. >>>>>*However*, if you feel like it, you can still do it yourself with some
  29. >>>>>clever mallocs and overlaying structures.  Not exactly elegant, but you
  30. >>>>>can make it pretty memory-efficient.
  31. >>>>
  32. >>>>Not exactly blessed by the C standard, either :-)
  33. >>>>
  34. >>>
  35. >>>Maybe not blessed, but it's certainly not condemned.
  36. >>
  37. >>You'll almost certainly invoke undefined behaviour as far as the language
  38. >>is concerned. So it is in the same category as void main() or i = i++,
  39. >>i.e. condemned to be non-portable.
  40. >>
  41. >
  42. >You're willing to make a broad generalization, but you aren't backing it
  43. >up with any substance.  *Why* will it almost certainly invoke undefined
  44. >behavior?  You're allowed to treat memory like a series of bytes or
  45. >chars, and that's *extremely* well-defined.
  46.  
  47. It is defined in as much as you are guaranteed not to have aliasing problems.
  48. What the results are are not at all clearly defined.
  49.  
  50. The statement was about overlaying structures. That will almost certainly
  51. invoke undefined behaviour e.g. if I access an object of type struct a
  52. through an lvalue of type struct b then that results in undefined behaviour,
  53. even if the member lists of struct a and struct b are identical (the only
  54. instance where this is well defined is if a variable with external linkage
  55. is defined as struct a in one translation unit and struct b in another).
  56. In terms ofaccessing the structure members the following is significant from
  57. the standard 6.3.2.3:
  58.  
  59. "If a union contains several structures that share a common initial sequence
  60.  (see below), and if the union object currently contains one of these
  61.  structures, it is permitted to inspect the common initial part of any of
  62.  them. Two strcutures share a commom initial sequence if corresponding members
  63.  have compatible types (and, for bit-fields, the same widths) for a sequence
  64.  of one or more initial members".
  65.  
  66. So there are circumstances where it is legal but not when you are accessing
  67. a member through another with incompatible type.
  68.  
  69. > Of course, treating it
  70. >correctly is always up to the programmer, but there's no reason why a
  71. >programmer will "almost certainly" do it wrong.
  72.  
  73. I'm not saying that the code won't work on a particular implementation (or
  74. even most implementations), simply that it very likely to result in undefined
  75. behaviour as far as the C language is concerned.
  76.  
  77. >In fact, it sounds like you agree with me a little later in your post:
  78. >
  79. >>The language does give license to access any object through character
  80. >>lvalues. It has been suggested that future revisions of the standard will
  81. >>clarify the issue so that, say copying an object as an array of unsigned char
  82. >>works (as long as you end up with suitable alignment). Anyway this is a better
  83. >>approach than using unions.
  84. >
  85. >See?
  86.  
  87. I see that that approach avoids the issues of using structures and unions.
  88.  
  89. >>That doesn't mean that ANSI C will guarantee that your code does what
  90. >>you expect.
  91. >>
  92. >
  93. >Of course not.  In fact, relying on variables names to understand code
  94. >doesn't always give you what you expect either.
  95.  
  96. The expectations were fairly clear in this case.
  97.  
  98. -- 
  99. -----------------------------------------
  100. Lawrence Kirby | fred@genesis.demon.co.uk
  101. Wilts, England | 70734.126@compuserve.com
  102. -----------------------------------------
  103.